package com.teamin.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.core.util.StringUtils;
import com.teamin.config.ErrorCode;
import com.teamin.entity.MessageEntity;
import com.teamin.entity.UserEntity;
import com.teamin.service.MessageService;
import com.teamin.service.UserService;
import com.teamin.web.exception.GlobalErrorInfoException;
import com.teamin.web.model.ErrorInfo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 查看用户
 * 修改用户
 * @author 31484
 *
 */
@RestController
@RequestMapping("/v1")
@Api(description = "用户相关接口",tags = "用户")
public class UserController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserService userService;

	@Autowired
	private MessageService messageService;


	/**
	 * 查看用户
	 * @param userId
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/users/{userId}", method = RequestMethod.GET)
	public UserEntity getUser(@PathVariable("userId") Long userId,HttpServletRequest request) throws Exception{
		if (!(""+getLoginUserId(request)).equals(userId )) {
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST	, ErrorCode.FORBIDDEN_VIEW_USER));
		}
		return userService.selectUser(userId);
	}
	
	
	/**
	 * 修改用户
	 * @param user
	 * @return
	 * @throws GlobalErrorInfoException 
	 */
	@RequestMapping(value = "/users/{userId}", method = RequestMethod.PUT)
	public UserEntity updateUser(@PathVariable("userId") Long userId,@ModelAttribute UserEntity user,HttpServletRequest request) throws GlobalErrorInfoException {
		if (userId != getLoginUserId(request)) {
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST	, ErrorCode.FORBIDDEN_UPDATE_USER));
		}
		userService.updateUser(user);		
		return userService.selectUser(user.getUserId());
	}
	
	
	/**
	 * 添加用户
	 * @param user
	 * @return
	 * @throws GlobalErrorInfoException 

	@RequestMapping(value = "/users", method = RequestMethod.POST)*/
	public UserEntity addUser(@ModelAttribute UserEntity user,@RequestParam(required = true) String userName) throws GlobalErrorInfoException {
		if (true)
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.UNFINISH_METHORD));
		long userId = userService.addUser(user);
		return userService.selectUser(userId);
	}

	/**
	 * 增加表单ID
	 * @param fromId 表单ID
	 * @return
	 * @throws GlobalErrorInfoException
	 */
	@RequestMapping(value = "/user/action/fromid/{fromId}", method = RequestMethod.POST)
	public void addUserFromId(@PathVariable("fromId") String fromId,@RequestAttribute("loginUserId") Long loginUserId) throws GlobalErrorInfoException {
		if(StringUtils.isNullOrEmpty(fromId)) {
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FROM_ID_NOT_NULL));
		}
		userService.addSmallFromId(loginUserId,fromId);
	}

	/**
	 * 一个circle下的全部消息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/user/messages", method = RequestMethod.GET)
	public List<MessageEntity> getMessages(@RequestAttribute("loginUserId") Long loginUserId,Integer readState) throws Exception {

		if(readState==null || !(readState==1 || readState==0)){
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.CHECK_PARAMETER_FALSE));
		}

		List<MessageEntity> messageEntities = messageService.getMessageByUser(loginUserId,readState);


		return messageEntities ;
	}

	/**
	 * 获取用户还有多少条未读消息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/user/unreadcount", method = RequestMethod.GET)
	public JSONObject getUnreadCount(@RequestAttribute("loginUserId") Long loginUserId) throws Exception {

		JSONObject json =new JSONObject() ;
		json.put("unreadCount",userService.getUnreadCount(loginUserId));
		return json ;
	}
}